package com.awenhui.demo.core.utils.zutils;

import com.awenhui.demo.core.base.ActionResult;
import com.awenhui.demo.core.base.ResultCode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.MediaType;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.cors.CorsConfiguration;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * http处理工具类
 *
 * @author zhouxuanhong
 * @date 2019-04-03 14:11
 * @since JDK1.8
 */
public class ServletUtils {
    /**
     * 获取ServletRequestAttributes
     *
     * @return ServletRequestAttributes ${@link ServletRequestAttributes}
     * @author zhouxuanhong
     * @date 2019-04-03 18:45
     */
    private static ServletRequestAttributes getServletRequestAttributes() {

        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }


    /**
     * 获取HttpServletRequest
     *
     * @return HttpServletRequest ${@link HttpServletRequest}
     * @author zhouxuanhong
     * @date 2019-04-03 14:13
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes = getServletRequestAttributes();
        if (servletRequestAttributes == null) {
            return null;
        }
        return servletRequestAttributes.getRequest();
    }


    /**
     * 获取HttpServletResponse
     *
     * @return HttpServletResponse ${@link HttpServletResponse}
     * @author zhouxuanhong
     * @date 2019-04-03 14:14
     */
    public static HttpServletResponse getResponse() {
        ServletRequestAttributes servletRequestAttributes = getServletRequestAttributes();
        if (servletRequestAttributes == null) {
            return null;
        }
        return servletRequestAttributes.getResponse();
    }


    /**
     * 获取HttpSession
     *
     * @return HttpSession ${@link HttpSession}
     * @author zhouxuanhong
     * @date 2019-05-15 14:50
     */
    public static HttpSession getSesson() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return getRequest().getSession();
    }


    /**
     * 获取ServletContext
     *
     * @return ServletContext ${@link ServletContext}
     * @author zhouxuanhong
     * @date 2019-05-15 14:50
     */
    public static ServletContext getServletContext() {
        WebApplicationContext currentWebApplicationContext = ContextLoader.getCurrentWebApplicationContext();
        if (currentWebApplicationContext == null) {
            return null;
        }
        return ContextLoader.getCurrentWebApplicationContext().getServletContext();
    }


    /**
     * 响应输出
     *
     * @param resultCode ${@link ActionResult} 响应枚举
     * @param response   ${@link HttpServletResponse}
     * @throws IOException ${@link IOException}
     */
    public static void writerResponse(ResultCode resultCode, HttpServletResponse response) throws IOException {
        Integer code = resultCode.getValue();
        String message = resultCode.getDescription();
        writerResponse(code, message, response);
    }


    /**
     * 响应输出
     *
     * @param code     ${@link Integer} 响应代码
     * @param message  ${@link String} 响应消息
     * @param response ${@link HttpServletResponse}
     * @author zhouxuanhong
     * @date 2019-05-15 15:21
     */
    public static void writerResponse(Integer code, String message, HttpServletResponse response) throws IOException {
        writerResponse(code, message, null, response);
    }


    /**
     * 响应输出
     *
     * @param code     ${@link Integer} 响应代码
     * @param message  ${@link String} 响应消息
     * @param model    ${@link T} 响应数据
     * @param response ${@link HttpServletResponse}
     * @author zhouxuanhong
     * @date 2019-05-15 15:21
     */
    public static <T> void writerResponse(Integer code, String message, T model, HttpServletResponse response) throws IOException {
        ActionResult<T> actionResult = new ActionResult<>();
        if (code == 0) {
            actionResult.setSuccess(true);
        } else {
            actionResult.setSuccess(false);
        }
        actionResult.setMessage(message);
        actionResult.setCode(code);
        actionResult.setData(model);
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        response.setHeader("Access-Control-Allow-Origin", CorsConfiguration.ALL);
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
        response.setHeader("Access-Control-Max-Age", "3600");
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writeValue(response.getOutputStream(), actionResult);
    }
}
